﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Scada.Model.Entity;
using Scada.Client.SL.CommClass;
using Scada.Client.SL.ScadaDeviceService;
using Scada.Client.SL.SystemManagerService;

namespace Scada.Client.SL
{
    public partial class App : Application
    {
        /// <summary>
        /// 系统当前用户
        /// </summary>
        public static User CurUser = null;
        public static List<MenuTree> CurrMenu = null;
        public static SystemGlobalParameter SysGlobalPar = null;
        private SystemManagerServiceSoapClient _systemManagerServiceSoapClient;

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;
            InitializeComponent();
           
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            //初始化系统参数
            InitSysGlobalPar();
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // 如果应用程序是在调试器外运行的，则使用浏览器的
            // 异常机制报告该异常。在 IE 上，将在状态栏中用一个 
            // 黄色警报图标来显示该异常，而 Firefox 则会显示一个脚本错误。
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // 注意: 这使应用程序可以在已引发异常但尚未处理该异常的情况下
                // 继续运行。 
                // 对于生产应用程序，此错误处理应替换为向网站报告错误
                // 并停止应用程序。
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 初始化全局参数
        /// </summary>
        void InitSysGlobalPar()
        {
            //全局访问参数
            this._systemManagerServiceSoapClient = ServiceManager.GetSystemManagerService();
            this._systemManagerServiceSoapClient.GetSystemGlobalParameterCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    App.SysGlobalPar = BinaryObjTransfer.BinaryDeserialize<SystemGlobalParameter>(e.Result);
                    if (App.SysGlobalPar == null)
                    {
                        ScadaMessageBox.ShowWarnMessage("系统参数获取失败！", "警告信息");
                    }
                    else { this.RootVisual = new LoginPage(); }
                }
                else
                    ScadaMessageBox.ShowWarnMessage("系统参数获取失败！", "警告信息");
            };
            this._systemManagerServiceSoapClient.GetSystemGlobalParameterAsync();
        }
    }
}
